################################################################################################
# This script takes the clean SV data and QV data and recodes the preferences as follows:
# 
# - If a subject voted in favor (against) but assigned no points, the preferences is changed
#   to +1 (-1) in both samples. 
# - If a subject abstained but assigned points then the preference is set to zero in both samples.
# 
# Author: Luis S.
# Last Modified: 8.9.17
################################################################################################

library(dplyr)


#####################
###### SV Data ######
####################%

SV_data <- read.csv("California Stage 2 - SV & QV Data/Clean Data/California_MTurk_SV_Stage_2_clean.csv", header = T, stringsAsFactors = F)

# Changes the points that subject assigned under certain boundary conditions

for(row in 1:nrow(SV_data))
{
  # If subject voted In Favor/Against but assigned 0 pts, it changes it to +/- 1 point. 
  # If subjects abstains with points then it sets the preferences to zero. 
  
  ### Bilingual Education ###
  
  if((SV_data$Ref1_BilingualEduc[row] == "InFavor") & (SV_data$EducationPref[row] == 0)) {SV_data$EducationPref[row] <- 1}
  if((SV_data$Ref1_BilingualEduc[row] == "Against") & (SV_data$EducationPref[row] == 0)) {SV_data$EducationPref[row] <- 1}
  if((SV_data$Ref1_BilingualEduc[row] == "Abstain") & (SV_data$EducationPref[row] > 0)) {SV_data$EducationPref[row] <- 0}
  
  if(SV_data$Ref1_BilingualEduc[row] == "Against") {SV_data$EducationPref[row] <- SV_data$EducationPref[row]*-1}
  
  ### Teacher Tenure ###
  
  if((SV_data$Ref2_TeacherTenure[row] == "InFavor") & (SV_data$TeachersPref[row] == 0)) {SV_data$TeachersPref[row] <- 1}
  if((SV_data$Ref2_TeacherTenure[row] == "Against") & (SV_data$TeachersPref[row] == 0)) {SV_data$TeachersPref[row] <- 1}
  if((SV_data$Ref2_TeacherTenure[row] == "Abstain") & (SV_data$TeachersPref[row] > 0)) {SV_data$TeachersPref[row] <- 0}
  
  if(SV_data$Ref2_TeacherTenure[row] == "Against") {SV_data$TeachersPref[row] <- SV_data$TeachersPref[row]*-1}
  
  ### Public Bonds ###
  
  if((SV_data$Ref3_PublicBonds[row] == "InFavor") & (SV_data$BondsPref[row] == 0)) {SV_data$BondsPref[row] <- 1}
  if((SV_data$Ref3_PublicBonds[row] == "Against") & (SV_data$BondsPref[row] == 0)) {SV_data$BondsPref[row] <- 1}
  if((SV_data$Ref3_PublicBonds[row] == "Abstain") & (SV_data$BondsPref[row] > 0)) {SV_data$BondsPref[row] <- 0}
  
  if(SV_data$Ref3_PublicBonds[row] == "Against") {SV_data$BondsPref[row] <- SV_data$BondsPref[row]*-1}
  
  ### Immigration ###
  
  if((SV_data$Ref4_Immigration[row] == "InFavor") & (SV_data$ImmigrationPref[row] == 0)) {SV_data$ImmigrationPref[row] <- 1}
  if((SV_data$Ref4_Immigration[row] == "Against") & (SV_data$ImmigrationPref[row] == 0)) {SV_data$ImmigrationPref[row] <- 1}
  if((SV_data$Ref4_Immigration[row] == "Abstain") & (SV_data$ImmigrationPref[row] > 0)) {SV_data$ImmigrationPref[row] <- 0}
  
  if(SV_data$Ref4_Immigration[row] == "Against") {SV_data$ImmigrationPref[row] <- SV_data$ImmigrationPref[row]*-1}
}

write.csv(SV_data, "California Stage 2 - SV & QV Data/Clean Data/California_MTurk_SV_Stage_2_clean_prefrecoded.csv", row.names = F)



#####################
###### QV Data ######
####################%

QV_data <- read.csv("California Stage 2 - SV & QV Data/Clean Data/California_MTurk_QV_Stage_2_clean.csv", header = T, stringsAsFactors = F)

# Changes the points that subject assigned under certain boundary conditions

for(row in 1:nrow(QV_data))
{
  # If subject voted In Favor/Against but assigned 0 pts, it changes it to 1 point. 
  # If subjects abstains with points then it sets the preferences to zero. 
  
  ### Bilingual Education ###
  
  if((QV_data$Ref1_BilingualEduc[row] == "InFavor") & (QV_data$EducationPref[row] == 0)) {QV_data$EducationPref[row] <- 1}
  if((QV_data$Ref1_BilingualEduc[row] == "Against") & (QV_data$EducationPref[row] == 0)) {QV_data$EducationPref[row] <- 1}
  if((QV_data$Ref1_BilingualEduc[row] == "Abstain") & (QV_data$EducationPref[row] > 0)) {QV_data$EducationPref[row] <- 0}
  
  if(QV_data$Ref1_BilingualEduc[row] == "Against") {QV_data$EducationPref[row] <- QV_data$EducationPref[row]*-1}
  
  ### Teacher Tenure ###
  
  if((QV_data$Ref2_TeacherTenure[row] == "InFavor") & (QV_data$TeachersPref[row] == 0)) {QV_data$TeachersPref[row] <- 1}
  if((QV_data$Ref2_TeacherTenure[row] == "Against") & (QV_data$TeachersPref[row] == 0)) {QV_data$TeachersPref[row] <- 1}
  if((QV_data$Ref2_TeacherTenure[row] == "Abstain") & (QV_data$TeachersPref[row] > 0)) {QV_data$TeachersPref[row] <- 0}
  
  if(QV_data$Ref2_TeacherTenure[row] == "Against") {QV_data$TeachersPref[row] <- QV_data$TeachersPref[row]*-1}
  
  ### Public Bonds ###
  
  if((QV_data$Ref3_PublicBonds[row] == "InFavor") & (QV_data$BondsPref[row] == 0)) {QV_data$BondsPref[row] <- 1}
  if((QV_data$Ref3_PublicBonds[row] == "Against") & (QV_data$BondsPref[row] == 0)) {QV_data$BondsPref[row] <- 1}
  if((QV_data$Ref3_PublicBonds[row] == "Abstain") & (QV_data$BondsPref[row] > 0)) {QV_data$BondsPref[row] <- 0}
  
  if(QV_data$Ref3_PublicBonds[row] == "Against") {QV_data$BondsPref[row] <- QV_data$BondsPref[row]*-1}
  
  ### Immigration ###
  
  if((QV_data$Ref4_Immigration[row] == "InFavor") & (QV_data$ImmigrationPref[row] == 0)) {QV_data$ImmigrationPref[row] <- 1}
  if((QV_data$Ref4_Immigration[row] == "Against") & (QV_data$ImmigrationPref[row] == 0)) {QV_data$ImmigrationPref[row] <- 1}
  if((QV_data$Ref4_Immigration[row] == "Abstain") & (QV_data$ImmigrationPref[row] > 0)) {QV_data$ImmigrationPref[row] <- 0}
  
  if(QV_data$Ref4_Immigration[row] == "Against") {QV_data$ImmigrationPref[row] <- QV_data$ImmigrationPref[row]*-1}  
}

write.csv(QV_data, "California Stage 2 - SV & QV Data/Clean Data/California_MTurk_QV_Stage_2_clean_prefrecoded.csv", row.names = F)
